package com.zp.qualitypractice.Utils;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.provider.Settings;
import android.util.Pair;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Created by izp on 2017/6/27.
 */

public class ToastUtils {
//建立一个全局的toast对象
    private static Toast mToast;
    private static Handler mHandler = new Handler(Looper.getMainLooper());//获取主线程handler
//    这个Handler我觉得可以在Application中去维护，避免工具类中出现多个全局Handler

    /**
     * @param context
     * @param view
     * @return
     */
    public static Toast viewToast(Context context, View view){
        if (null==mToast){
            mToast = new Toast(context.getApplicationContext());
            mToast.setView(view);
            mToast.setGravity(Gravity.CENTER,0,0);
            mToast.show();
            return mToast;
        }else {
            mToast.setView(view);
            mToast.show();
            return mToast;
        }
    }
    /**
     * 设置mToast字体及背景
     * @param messageColor
     * @param background
     * @return
     */
    public void setToastBackground(Toast toast,int messageColor, int background) {
        View view = toast.getView();
        if(view!=null){
            TextView message=((TextView) view.findViewById(android.R.id.message));
            message.setBackgroundResource(background);
            message.setTextColor(messageColor);
        }
    }


    /**
     * @param context
     * @param msg
     * 连续点击一个按钮，缓存一个Toast，每次都调用show方法（推荐写法，体验比较好）
     */
    public static void showToast(Context context,String msg){
        if (null==mToast){
            mToast = Toast.makeText(context,msg,Toast.LENGTH_SHORT);
        }else {
            mToast.setText(msg);
        }
        mToast.show();
    }

    /**
     * @param context
     * @param msg
     *先隐藏toast再展示
     */
    public static void showCancelToast(Context context,String msg){
        if (null==mToast){
            mToast = Toast.makeText(context,msg,Toast.LENGTH_SHORT);
        }else {
            mToast.setText(msg);
        }
        mToast.cancel();
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                mToast.show();
            }
        },100);

    }


    /**
     * @param context
     * @param msg
     * 实现了一个防止内容重复弹出的Toast工具类，防止内容重复弹出，并且新的Toast会立刻覆盖旧的Toast，不会排队等待就Toast显示结束才显示
     */
    public static void shortToast(Context context,String msg){
        if (null==mToast){
//            mToast = Toast.makeText(context,msg,Toast.LENGTH_SHORT);
//            这种放入容易导致内存泄露，所以我这里使用了另一种方法
            mToast = Toast.makeText(context.getApplicationContext(),msg,Toast.LENGTH_SHORT);
//            当然也可以使用全局的applicatinContext
            long lastShow = System.currentTimeMillis();
            mToast.show();
            View view = mToast.getView();
            if (null!=view)
            view.setTag(new Pair<String,Long>(msg, lastShow));
        }else {
            String oldMsg = "";
            long lastShow = System.currentTimeMillis();
            View view = mToast.getView();
            if (null!=view){
                Pair<String,Long> pair = (Pair<String, Long>) view.getTag();
                if (null!=pair){
                    oldMsg = pair.first;
                    lastShow = pair.second;
                }
            }
            if (msg.equals(oldMsg)){//如果内容重复，那么下一次显示内容必须等待上一次显示完毕，即(twoTime-oneTime)>Toast.LENGTH_SHORT
                if ((System.currentTimeMillis()-lastShow)>Toast.LENGTH_SHORT){
                    mToast.show();
                }
            }else {//如果内容不重复，那么立即设置新的内容并显示
                mToast.setText(msg);
                mToast.show();
            }
        }

    }
}
